import { createWebHashHistory, createRouter } from 'vue-router'
import Home from './views/Home.vue'
import Doc from './views/Doc.vue'
import Button from './components/Button.vue'
import Dialog from './components/Dialog.vue'
import Switch from './components/Switch.vue'
import Tabs from './components/Tabs.vue'
import Form from './components/Form.vue'

import { h } from 'vue'
import Markdown from './components/Markdown.vue'
import intro from './markdown/intro.md'
import getStart from './markdown/get-started.md'
import install from './markdown/install.md'
import { scrollTo } from './utils/scrollTo'
// const md = (filename) => h(Markdown, { path: `../markdown/${filename}.md`, key: filename })
const md = (string, key) => h(Markdown, { content: string, key })

// 创建并添加 hash 模式路由
const history = createWebHashHistory()
export const router = createRouter({
  history,
  routes: [
    {
      path: '/',
      component: Home,
    },
    {
      path: '/doc',
      component: Doc,
      children: [
        { path: '', redirect: '/doc/intro' },
        {
          path: 'intro',
          component: md(intro, 'intro'),
        },
        {
          path: 'get-start',
          component: md(getStart,'get-started'),
        },
        {
          path: 'install',
          component: md(install, 'install'),
        },
        { path: 'button', component: Button },
        { path: 'dialog', component: Dialog },
        { path: 'switch', component: Switch },
        { path: 'tabs', component: Tabs },
        { path: 'form', component: Form },
      ],
    },
  ],
})

router.beforeEach((to, from, next) => {
  scrollTo(0, 800)
  next()
})
